#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_set>
#include<bits/stdc++.h>
#define fi first
#define se second
#define SIZE(x) ((int)x.size())
#define out(x) cerr<<#x<<" = "<<(x)<<endl;
#define MP(a,b) make_pair(a,b)
using namespace std;
typedef long long ll;
#define VI vector<int>
#define VVI vector< vector<int> >
#define VC vector<char>
#define PI pair<int,int>
#define PL pair<ll,ll>
#define EPS 0.0000001


inline int lson(int x){return x<<1;}
inline int rson(int x){return (x<<1)|1;}

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

template<typename T> 
inline void read(T &x){
	x=0;T f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

//#define INF 5340172838076674L
#define INF 1000000000
#define MOD 998244353
#define W 20
ll n,q;
unsigned s;
inline unsigned get(unsigned x){
	x ^= x << 13;
	x ^= x >> 17;
	x ^= x << 5;
	return s = x; 
}
struct {
	int to,ne;
}e[500005];
int esum=0;
int dep[500005];
int fa[500005];
int chs[500005];
int son[500005];
int head[500005];
int top[500005];
void add(int a,int b){
	++esum;
	e[esum].ne=head[a];
	head[a]=esum;
	e[esum].to=b;
}

void dfs1(int x,int d,int f){
	fa[x]=f;
	dep[x]=d++;
	chs[x]=1;
	int tmp,hs=0;
	for(int i=head[x];i;i=e[i].ne){
		tmp=e[i].to;
		dfs1(tmp,d,x);
		chs[x]+=chs[tmp];
		if(!hs||chs[tmp]>chs[hs])hs=tmp;
	}
	son[x]=hs;
}
int id[500005];
int mp[500005];
int cnt=0;
void dfs2(int x,int f){
	id[x]=++cnt;
	mp[cnt]=x;
	top[x]=f;
	if(son[x])dfs2(son[x],f);
	int tmp;
	for(int i=head[x];i;i=e[i].ne){
		tmp=e[i].to;
		if(tmp==son[x])continue;
		dfs2(tmp,tmp);
	}
}

int go(int x,int k){
	while(k&&x){
		if(id[x]-id[top[x]]>=k)return mp[id[x]-k];
		k-=id[x]-id[top[x]]+1;
		x=fa[top[x]];
	}
	return x;
}

int main(){
    #ifdef BUG
    freopen("a.in","r",stdin);
    #endif
    n=read();q=read();read(s);
	int a,r;
	memset(head,0,sizeof head);
	for(int i=1;i<=n;i++){
		a=read();
		if(a==0)r=i;
		add(a,i);
	}
	dfs1(r,1,0);
	dfs2(r,r);
	ll res=0,x=0,k,ans=0;
	for(int i=1;i<=q;i++){
		x=((get(s)^ans)%n)+1;
		k=(get(s)^ans)%dep[x];
		//cout<<x<<' '<<k<<endl;
		ans=go(x,k);
		res^=i*ans;
		//cout<<ans<<endl;
	}
	cout<<res<<endl;
}